/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.gdp;

import java.util.Comparator;
import java.util.List;

import twaver.Element;
import twaver.TWaverConst;
import twaver.table.TElementTable;
import twaver.table.TTableColumn;
import twaver.table.TTableModel;

public class SortComparator implements Comparator{
	
	private TElementTable table;
	
	public SortComparator(TElementTable table){
		this.table = table;
	}
	
	public int compare(Object o1, Object o2) {
		List sortColumns = table.getSortColumnList();
		if(sortColumns.size() > 0){
			Element e1 = (Element)o1;
			Element e2 = (Element)o2;
			TTableColumn column = (TTableColumn)sortColumns.get(0);
			String name = column.getName();
			int flag = TTableModel.SORT_ASCENDING == column.getSortMode() ? 1 : -1;
			if("name".equals(name)){
				return flag * (e1.getName().compareTo(e2.getName()));
			}
			if((TWaverConst.CLIENT_PROPERTY_PREFIX + "chart.value").equals(name)){
				return flag * (Double.compare(e1.getChartValue(), e2.getChartValue()));
			}
		}
		return 0;
	}
}